﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace EBA.IoC
{
    /// <summary>
    /// 导出约定
    /// </summary>
    public sealed class ExportConvention
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contractType"></param>
        /// <param name="componentType"></param>
        public ExportConvention(Type contractType, Type componentType)
            : this(contractType, null, componentType)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contractType"></param>
        /// <param name="contractName"></param>
        /// <param name="componentType"></param>
        public ExportConvention(Type contractType, string contractName, Type componentType)
        {
            this.ContractType = contractType;
            this.ContractName = contractName;
            this.ComponentType = componentType;
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string ContractName { get; private set; }

        /// <summary>
        /// 契约类型
        /// </summary>
        public Type ContractType { get; private set; }

        /// <summary>
        /// 实际类型
        /// </summary>
        public Type ComponentType
        {
            get;
            private set;
        }

        public ExportAttribute ExportAttribute { get; set; }

        /// <summary>
        /// Hashcode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.ContractName == null? string.Empty.GetHashCode():this.ContractName.GetHashCode() ^ this.ContractType.GetHashCode() ^ this.ComponentType.GetHashCode();
        }

        /// <summary>
        /// 与另一个对象比较值是否相等
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            var convertion = obj as ExportConvention;
            return convertion == null ? false :convertion.ContractName == this.ContractName 
                && convertion.ComponentType == this.ComponentType 
                && convertion.ContractType == this.ContractType;
        }
    }
}
